<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录账号</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #f4f4f4;
        }
        form {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            width: 300px;
        }
        input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
        button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        p {
            text-align: center;
            margin-top: 10px;
        }
        .error {
            color: red;
        }
    </style>
</head>
<body>
<form id="${pageContext.request.contextPath}/loginForm" action="user" method="post">
    <h2>欢迎登录</h2>
    <input type="hidden" name="action" value="login">
    <input type="email" id="email" name="email" placeholder="邮箱" required>
    <input type="password" id="password" name="password" placeholder="密码" required>
    <p class="error" id="errorMessage"></p>
    <p>没有账号？<a href="/register.jsp">立即注册</a></p>
    <p>如果你是管理员<a href="html/conservator/login.jsp">请点击这里</a></p>
    <button type="submit">登录</button>
</form>
<script>
    const urlParams = new URLSearchParams(window.location.search);
    const error = urlParams.get('error');
    if (error === 'empty') {
        document.getElementById('errorMessage').textContent = '邮箱和密码不能为空！';
    } else if (error === 'invalid') {
        document.getElementById('errorMessage').textContent = '用户未注册或密码错误！';
    }

    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();
        let isValid = true;
        // 验证邮箱
        const email = document.getElementById('email').value;
        const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        if (!emailRegex.test(email)) {
            document.getElementById('email').classList.add('error');
            document.getElementById('emailError').textContent = '请输入有效的邮箱地址';
            isValid = false;
        } else {
            document.getElementById('email').classList.remove('error');
            document.getElementById('emailError').textContent = '';
        }

        // 验证密码（简单检查非空，完整验证应在后端）
        const password = document.getElementById('password').value;
        if (password.length < 8) {
            document.getElementById('password').classList.add('error');
            document.getElementById('passwordError').textContent = '密码长度至少为8位';
            isValid = false;
        } else {
            document.getElementById('password').classList.remove('error');
            document.getElementById('passwordError').textContent = '';
        }

        if (isValid) {
            this.submit();
        }
    });
</script>
</body>
</html>